﻿using System;
using System.Drawing;
using System.Windows.Forms;
using System.Configuration;
using OneMake.DataAccess;
using OneMake.Winforms;
using System.Reflection;
using OneMake.Model;

namespace OneMake.Winforms
{
    public partial class MainForm : Form
    {
        private bool bUpdateChecked = false;
        private bool bCanExit = false;
        private StatisticsControl statCtrl = new StatisticsControl();
        private DataHelper dataHelper = new DataHelper();
        private IBaseReportCtrl reportCtrl = null;
        public MainForm()
        {
            InitializeComponent();
        }

        private void MainForm_Shown(object sender, EventArgs e)
        {


        }

        private void MainForm_Load(object sender, EventArgs e)
        {
            treeView1.ExpandAll();
            treeView1.SelectedNode = treeView1.Nodes[0];

            statCtrl.Parent = panel3;
            statCtrl.Dock = DockStyle.Fill;

            //隐藏tab标签;
            //this.tabControlFunc.Region = new Region(new RectangleF(this.tabControlFunc.Left, this.tabControlFunc.Top, this.tabControlFunc.Width, this.tabControlFunc.Height));
        }

        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            bCanExit = true;
            Close();
        }

        private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (bCanExit)
            {
                if (MessageBox.Show("请确认是否退出应用程序? ", "确认 ", MessageBoxButtons.YesNo, MessageBoxIcon.Question) != DialogResult.Yes)
                {
                    e.Cancel = true;
                }
                else
                {
                    e.Cancel = false;
                }
            }
            else
            {
                this.WindowState = FormWindowState.Minimized;
            }
        }

        private void menuShopMgr_Click(object sender, EventArgs e)
        {
            BaseDataForm form = new BaseDataForm();
            form.tabIndex = 1;
            form.ShowDialog(this);
        }

        private void menuEmployeeMgr_Click(object sender, EventArgs e)
        {
            BaseDataForm form = new BaseDataForm();
            form.tabIndex = 2;
            form.ShowDialog(this);
        }

        private void menuDataImport_Click(object sender, EventArgs e)
        {
            dataHelper.WriteLog(0, 0, "进入数据导入界面");
            DataManageForm form = new DataManageForm();
            form.ShowDialog(this);
            dataHelper.WriteLog(0, 0, "退出数据导入界面");
        }

        private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
        {
            if (treeView1.SelectedNode != null)
            {
                toolStripStatusLabel1.Text = treeView1.SelectedNode.Text;
                lblCurReport.Text = treeView1.SelectedNode.Text;

                if (treeView1.SelectedNode.Tag != null)
                {
                    string Tag = treeView1.SelectedNode.Tag.ToString();
                    if (Tag == "X")
                    {
                        //MessageBox.Show("暂未实现......", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                    else if (Tag.Substring(0, 6) == "report")
                    {
                        toolStripStatusLabel1.Text = Tag;
                        PopulateReport(Tag.ToString());
                    }
                    else
                    {
                        toolStripStatusLabel1.Text = Tag;
                        string tableName = Tag;
                        tabControlFunc.SelectedTab = tabPageReport;
                        statCtrl.UpdateView(tableName);
                    }
                }
            }
        }
        private void PopulateReport(string reportName)
        {
            tabControlFunc.SelectedTab = tpReport;
            if (reportCtrl != null)
            {
                if (reportCtrl.ReportName == reportName)
                    return;
                else
                {
                    tpReport.Controls.Clear();
                    reportCtrl = null;
                }
            }

            Assembly asmb = Assembly.LoadFrom("AppCore.exe");
            reportCtrl = (IBaseReportCtrl)asmb.CreateInstance(string.Format("OneMake.Winforms.uc_{0}", reportName));
            reportCtrl.Parent2 = tpReport;
            reportCtrl.Dock2 = DockStyle.Fill;
        }

        private void menuCalc_Click(object sender, EventArgs e)
        {
            System.Diagnostics.Process.Start("calc.exe");
        }

        private void menuExcel_Click(object sender, EventArgs e)
        {
            System.Diagnostics.Process.Start("excel.exe");
        }

        private void menuOptions_Click(object sender, EventArgs e)
        {
            MessageBox.Show("应用程序选项设置", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }

        private void tabControlFunc_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (tabControlFunc.SelectedTab == tabPageGraphic)
            {
                //this.Chart1.DataSource = statCtrl.GetDataSource();
                //Chart1.Series["Default"].XValueMember = "编号";
                //Chart1.Series["Default"].YValueMembers = "导入日期";
                //this.Chart1.DataBind();
            }
        }

        private void menuAbout_Click(object sender, EventArgs e)
        {
            string ver = System.Reflection.Assembly.GetExecutingAssembly().GetName().Version.ToString();
            MessageBox.Show("网美科技 - 数据报表工具" + ver, "关于", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }

        private void menuImageImport_Click(object sender, EventArgs e)
        {
            //ImagesImport form = new ImagesImport();
            //form.ShowDialog(this);
        }
 
        private void menuCheckUpdate_Click(object sender, EventArgs e)
        {
            bUpdateChecked = true;
            //ConfigURL = http://127.0.0.1/gallery/update/version.xml
            //UpdateUrl = http://127.0.0.1/gallery/update/
            String AutoUpdateUrl = ConfigurationManager.AppSettings["AutoUpdateUrl" + UserSession.Network];
            if (AutoUpdateUrl.EndsWith("version.xml"))
            {
                this.autoUpdater.ConfigURL = AutoUpdateUrl;
                //去掉version.xml;
                this.autoUpdater.UpdateUrl = AutoUpdateUrl.Substring(0, AutoUpdateUrl.Length - 11);
            }
            else
            {
                this.autoUpdater.ConfigURL = AutoUpdateUrl + "version.xml";
                this.autoUpdater.UpdateUrl = AutoUpdateUrl;
            }


            this.autoUpdater.AutoDownload = false;
            this.autoUpdater.AutoRestart = true;
            
            this.autoUpdater.DownloadForm = new ConfirmForm(autoUpdater);
            this.autoUpdater.RestartForm = null;

            this.autoUpdater.TryUpdate();
        }

        private void menuBrowserImage_Click(object sender, EventArgs e)
        {
            //ImagesBrowse form = new ImagesBrowse();
            //form.ShowDialog(this);
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            timer1.Enabled = false;

            if (!bUpdateChecked)
                menuCheckUpdate_Click(sender, e);
        }


    }
}
